//
//  YZXStatusViewModel.swift
//  YZXWeibo
//
//  Created by 叶周翔 on 16/7/23.
//  Copyright © 2016年 叶周翔. All rights reserved.
//

import UIKit

class YZXStatusViewModel: NSObject {
    /// 单例
    static let shareStatusVM:YZXStatusViewModel = YZXStatusViewModel()
    
    /// 表示vm是否在加载模型
    private var loadingStatus:Bool = false
    
    
    func loadStatus(since_id: Int64, max_id: Int64, completion:(error:NSError?,status:[YZXStatus]?) -> Void){
        
        /// 加载微博的数据
        // 判断是否有
        guard let access_token = YZXUserAccountVM.shareUserAccountVM.userAccount?.access_token else{
            // 如果条件不成立就返回
            return
        }
        let urlString = "2/statuses/home_timeline.json"
        var paraments: [String: AnyObject] = ["access_token":access_token]
        
        /**
        *  判断有没有传入since_id
        */
        if since_id > 0 {
            paraments["since_id"] = NSNumber(longLong: since_id)
        }else if max_id > 0 {
            paraments["max_id"] = NSNumber(longLong: max_id - 1)
        }
        
        
        
        YZXNetworkTool.shareNetworkTool.GET(urlString, parameters: paraments, progress: nil, success: { (_, responseObject) -> Void in
            
            // 根据字典的key获取字典数组
            let dicArr = responseObject!["statuses"] as? [[String: AnyObject]]
            // 字典转模型
            var status = [YZXStatus]()
            for dic in dicArr!{
                let statu = YZXStatus(dic: dic)
                status.append(statu)
            }
            // 闭包回调
            completion(error: nil, status: status)
            }) { (_, error) -> Void in
                // 闭包回调
                completion(error: error, status: nil)
                print(error)
        }
    }
    
    func sendStatus(status: String, image:UIImage?, completion:(error: NSError?,progress:NSProgress?) -> Void) {
        guard let access_token = YZXUserAccountVM.shareUserAccountVM.userAccount?.access_token else {
            return
        }
        // 参数
        let parameters = [
            "access_token": access_token,
            "status": status
        ]
        
        // 如果没有图片就发送纯文本的微博
        if image == nil {
            
            let urlString = "https://api.weibo.com/2/statuses/update.json"
           
            
            // 发送请求
            YZXNetworkTool.shareNetworkTool.POST(urlString, parameters: parameters, progress: nil, success: { (_, _) -> Void in
                completion(error: nil, progress: nil)
                }) { (_, error) -> Void in
                    completion(error: error,progress: nil)
            }
        }else{
           // 有图片就发送带图片的微博
            let urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
            
            // 向服务器上传数据
            /*
            ((AFMultipartFormData) -> Void)? 需要向服务器上传文件的二进制数据
            
            */
            
            let data = UIImagePNGRepresentation(image!)!
            
            YZXNetworkTool.shareNetworkTool.POST(urlString, parameters: parameters, constructingBodyWithBlock: { (formatData) -> Void in
                    formatData.appendPartWithFileData(data, name: "pic", fileName: "image", mimeType: "image/png")
                }, progress: { (progress) -> Void in
//                    print("\(progress)")
                    completion(error: nil, progress: progress)
                    
                }, success: { (_, result) -> Void in
                    completion(error: nil,progress: nil)
                    
                }, failure: { (_, error) -> Void in
                    completion(error: error,progress: nil)
                    
            })
            
            
        }
    }
}